package com.wt.hrentSystem.hrent.service;

import com.wt.hrentSystem.common.entity.Page;
import com.wt.hrentSystem.common.service.BaseService;
import com.wt.hrentSystem.hrent.dao.ApplyRentDao;
import com.wt.hrentSystem.hrent.entity.ApplyRent;
import com.wt.hrentSystem.hrent.entity.BikeRenting;
import com.wt.hrentSystem.hrent.entity.BikeResource;
import com.wt.hrentSystem.hrent.entity.Rent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Service("ApplyRentService")
public class ApplyRentService extends BaseService<ApplyRentDao, ApplyRent> {

    @Autowired
    private BikeResourceService bikeResourceService;

    @Autowired
    private BikeRentingService bikeRentingService;

    @Autowired
    private RentService rentService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ApplyRent get(String id) {
        ApplyRent applyRent = super.get(id);
        applyRent.setBikeResource(bikeResourceService.get(applyRent.getBikeId()));
        return applyRent;
    }



    /**
     * 查询多条数据
     * @param applyRent
     * @return
     */
    public List<ApplyRent> findList(ApplyRent applyRent){
        List<ApplyRent> list = super.findList(applyRent);
        for (ApplyRent rent : list) {
            rent.setBikeResource(bikeResourceService.get(rent.getBikeId()));
        }
        return list;
    }

    /**
     * 分页查询多条数据
     * @param page
     * @param applyRent
     * @return 消息集合
     */
    public Page<ApplyRent> findPage(Page page, ApplyRent applyRent) {
        Page page1 = super.findPage(page, applyRent);
        List<ApplyRent> list = page1.getList();
        for (ApplyRent rent : list) {
            rent.setBikeResource(bikeResourceService.get(rent.getBikeId()));
        }
        page1.setList(list);
        return page1;
    }


    /**
     * 删除数据
     * @param applyRent
     */

    public void delete(ApplyRent applyRent) {
        super.delete(applyRent);
    }

    /**
     * 更新或插入数据
     */
    public void save(ApplyRent applyRent){
        super.save(applyRent);
    }

    //查询个人的租车申请信息
    public Page<ApplyRent> findPersonalViewApply(Page<ApplyRent> page, ApplyRent applyRent) {
        if(applyRent.getPage()!=null && applyRent.getPage().getPageSize()>0){
            page.setPageSize(applyRent.getPage().getPageSize());
        }
        applyRent.setPage(page);
        List<ApplyRent> rentList = this.dao.findPersonalViewApplyPage(applyRent);
        for (ApplyRent view : rentList) {
            view.setBikeResource(bikeResourceService.get(view.getBikeId()));
        }
        page.setList(rentList);
        return page;
    }

    @Transactional
    public void paySuccess(ApplyRent applyRent) {
        //修改租车申请的订单信息
        applyRent.setState(3);
        applyRent.setStartTime(new Date());
        super.save(applyRent);
        //新增正在列表的数据
        ApplyRent applyRent1 = this.dao.get(applyRent.getId());

        BikeResource bikeResource = bikeResourceService.get(applyRent1.getBikeId());
        BikeRenting bikeRenting = new BikeRenting();
        bikeRenting.setBikeId(bikeResource.getId());
        bikeRenting.setStartTime(applyRent1.getStartTime());
        bikeRenting.setEndTime(applyRent1.getEndTime());
        bikeRenting.setTenantId(applyRent.getCurrentUser().getId());
        bikeRenting.setCurrentUser(applyRent.getCurrentUser());
        bikeRenting.setMoney(applyRent1.getMoney());
        bikeRentingService.save(bikeRenting);

        // 保存租金数据
        Rent rent = new Rent();
        rent.setPayableTime(new Date());
        rent.setPayTime(new Date());
        rent.setRentingId(applyRent1.getId());
        rent.setState(0);
        rent.setCreateBy(bikeResource.getCurrentUser());
        rent.setUpdateBy(bikeResource.getCurrentUser());
        rent.setCreateDate(new Date());
        rent.setUpdateDate(new Date());
        rent.setDelFlag(0);
        rent.setRentingId(bikeRenting.getId());
        rent.setTenantId(bikeRenting.getTenantId());
        /**
         * 租金列表id
         * 租客id
         */
        rentService.save(rent);

    }
}
